package com.cxs.rdbdemo.utils;

import ohos.app.Context;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;

public class DeviceUtil {
    private DeviceUtil () {}

    // 获取远程设备Id列表
    public static List<String> getDeviceList() {
        // 第一步：获取设备id
        List<DeviceInfo> deviceList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        // 判断组网设备是否为空
        if (deviceList.isEmpty()) {
            return null;
        }
        int deviceNum = deviceList.size();
        List<String> deviceIds = new ArrayList<>(deviceNum);
        List<String> deviceNames = new ArrayList<>(deviceNum);
        deviceList.forEach((device)->{
            deviceIds.add(device.getDeviceId());
            deviceNames.add(device.getDeviceName());
        });
        return deviceIds;
    }


    // 获取远程设备Id
    public static String getDeviceId() {
        List<String> deviceIds = getDeviceList();

        //  使用deviceIds的第一个元素，作为启动远程设备的目标Id
        String deviceIdStr = deviceIds.get(0);
        return deviceIdStr;
    }

    // 获取本地设备Id
    public static String getLocalDeviceId(Context context) {
        String localDeviceId = KvManagerFactory.getInstance()
                .createKvManager(new KvManagerConfig(context)).getLocalDeviceInfo().getId();
        return localDeviceId;
    }

    // 获取本地设备Type
    public static String getLocalDeviceType(Context context) {
        String localDeviceType = KvManagerFactory.getInstance()
                .createKvManager(new KvManagerConfig(context)).getLocalDeviceInfo().getType();
        return localDeviceType ;
    }
}